package com.lycx.businessmanager.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.lycx.businessmanager.constant.ConstantsDbOperation;
import com.lycx.businessmanager.constant.ConstantsMessage;
import com.lycx.businessmanager.constant.ConstantsTableName;
import com.lycx.businessmanager.dao.DictionaryDao;
import com.lycx.businessmanager.manager.DictionaryManager;
import com.lycx.businessmanager.manager.SequenceManager;
import com.lycx.businessmanager.model.Dictionary;
import com.lycx.businessmanager.service.IDictionaryService;

public class DictionaryServiceImpl implements IDictionaryService {

	private DictionaryDao dictionaryDao;

	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dictionary> select(Dictionary dictionary) {
		return (List<Dictionary>) dictionaryDao.select(ConstantsDbOperation.DICTIONARY_SELECT, dictionary);
	}

	@Override
	public String add(Dictionary dictionary) {
		String result = null;
		try {
			List<Dictionary> list = selectRepeat(dictionary);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.DICTIONARY_REPEAT_FAIL;
			} else {
				dictionary.setId(SequenceManager.createId(ConstantsTableName.DICTIONARY));
				dictionaryDao.insert(ConstantsDbOperation.DICTIONARY_INSERT, dictionary);
				result = ConstantsMessage.DICTIONARY_ADD_SUCCESS;
				DictionaryManager.setDictList(select(null));
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.DICTIONARY_ADD_FAIL;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private List<Dictionary> selectRepeat(Dictionary dictionary) {
		return (List<Dictionary>) dictionaryDao.select(ConstantsDbOperation.DICTIONARY_SELECT_REPEAT, dictionary);
	}

	@Override
	public String del(int[] ids) {
		String result = null;
		try {
			if (ids != null && ids.length > 0) {
				for (int id : ids) {
					Dictionary dictionary = new Dictionary();
					dictionary.setId(id);
					dictionaryDao.delete(ConstantsDbOperation.DICTIONARY_DELETE, dictionary);
				}
				result = ConstantsMessage.DICTIONARY_DEL_SUCCESS;
				DictionaryManager.setDictList(select(null));
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.DICTIONARY_DEL_FAIL;
		}
		return result;
	}

	@Override
	public String mod(Dictionary dictionary) {
		String result = null;
		try{
			List<Dictionary> list = selectRepeat(dictionary);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.DICTIONARY_REPEAT_FAIL;
			} else {
		        dictionaryDao.update(ConstantsDbOperation.DICTIONARY_UPDATE, dictionary);
		        result = ConstantsMessage.DICTIONARY_MOD_SUCCESS;
		        DictionaryManager.setDictList(select(null));
			}
		    
		}catch(Exception e){
			e.printStackTrace();
			result = ConstantsMessage.DICTIONARY_MOD_FAIL;
		}
        return result;
	}

	@Override
	public Dictionary selectOne(Dictionary dictionary) {
		return (Dictionary) dictionaryDao.selectOne(ConstantsDbOperation.DICTIONARY_SELECT_ONE, dictionary);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Dictionary> getDictTypeList(Dictionary dictionary) {
		List<Dictionary> result = null;
		List<String> dictTypeList = dictionaryDao.select(ConstantsDbOperation.DICTIONARY_SELECT_DICT_TYPE, dictionary);
		if(dictTypeList != null && dictTypeList.size() > 0){
			result = new ArrayList<Dictionary>();
			for(String dictType : dictTypeList){
				String[] dictTypes = StringUtils.split(dictType, "|");
				if(dictTypes.length > 0){
				    Dictionary dictionaryTmp = new Dictionary();
				    dictionaryTmp.setDictType(dictTypes[0]);
				    dictionaryTmp.setDictTypeName(dictTypes[1]);
				    result.add(dictionaryTmp);
				}
 			}
		}
		return result;
	}
}
